<?php
namespace app\admin\traits;

use think\facade\View;
use think\facade\Request;

trait Curd
{
    protected $_model=null;
    /**
     * [create 创建]
     * @return [type] [description]
     */
    public function create()
    {
        $data = $this->_model::create($this->data);
        if(!$data->id){
            $this->code = 0;
            $this->msg = '创建失败!';
        }
    	return $this->fetch();
    }
    /**
     * [update 更新]
     * @return [type] [description]
     */
    public function update()
    {
        $data = $this->totabledata($this->data);

        $info = ($this->_model)::find($data['id']);
        $res = $info->save($data);
        if($res!==true){
            $this->code = 0;
            $this->msg = '更新失败!';
        }
        return $this->fetch();
    }
    /**
     * [read 读取资源]
     * @return [type] [description]
     */
    public function read()
    {
        $id = Request::get('id');
        if($id){
            $data = ($this->_model)::find($id);
            $this->assign(get_defined_vars());
        }else{
            $this->status = 0;
        }
        echo '1';
        return $this->fetch();
    }
    public function edit()
    {
        $id = Request::get('id');
        if($id){
            $data = ($this->_model)::find($id);
            $this->assign(get_defined_vars());
        }else{
            $this->status = 0;
        }

        return $this->fetch();
    }
    public function add()
    {
        if(method_exists($this, '_add')){
            $this->_add();
        }
        return $this->fetch();
    }
    public function delete()
    {
        $id = Request::get('id');
        $obj=($this->_model)::find($id);
        $res = $obj->delete();
        if($res){
            $this->code = 1;
        }else{
            $this->code = 0;
        }
        return $this->fetch();
    }
    public function lists()
    {
        $this->setwhere();
        $count = $this->_model->count();
        $p = intval(Request::param('page'))?:1;
        $l = intval(Request::param('limit'))?:10;
        $o = $this->order();
        $data = $this->_model->page($p,$l)->order($o)->select();
        $this->code = 0;
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    protected function order()
    {
        return $this->autoorder();
    }
    protected function autoorder()
    {
        $filter = $this->totabledata($this->data);
        $order=[];
        if(!count($filter))
        {
            $order['id'] = 'desc';
        }
        
        foreach($filter as $k=>$v)
        {
            if($v!='')
            {
                $order["$k"] = "desc";
            }
        }
        return $order;
    }
    protected function setwhere()
    {
        $where = $this->filter();
        if(count($where)){
            return $this->_model=($this->_model)::where($where);
        }
        $this->_model = new $this->_model;
    }
    /**
     * [accessfield 可通过字段限制]
     * @return [type] [description]
     */
    protected function accessfield()
    {
    }
    /**
     * [filter 条件处理]
     * @return [type] [description]
     */
    protected function filter()
    {
        return $this->autofilter();
    }
    /**
     * [autofilter 自动生成查询条件]
     * @return [type] [description]
     */
    protected function autofilter()
    {
        $data = Request::param();
        $data = $this->totabledata($data);
        $rules = $this->filterrule();
        $where = [];
        foreach($data as $field=>$value){
            if(preg_match("/^\s+^/", $value)||$value=='')
            {
                continue;
            }
            if(!isset($rules[$field]))
            {
                $where[] = [$field,'=',$value];
            }else{
                $where[] = $this->parsewhere($value,$field,$rules[$field]);
            }
        }
        return $where;
    }
    protected function parsewhere($value,$field,$rule)
    {
        if($rule[0]=='like')
        {
            return [$field,'like','%'.$value.'%'];
        }
    }
    /**
     * [filterrule 筛选规则]
     * @return [type] [description]
     */
    protected function filterrule()
    {
        return [
            'name'=>['like'],
            'title'=>['like'],
            'realname'=>['like']
        ];
    }
    /**
     * [setmodel 设置curd模型]
     * @return [type] [description]
     */
    protected function setmodel()
    {
        $this->_model = $this->samemodel();
    }
    /**
     * [samemodel 获取和控制器名相同的模型名或者加s]
     * @return [type] [description]
     */
    protected function samemodel()
    {
        $controller = get_called_class();
        $model = '\\'.str_replace('\\controller\\', '\\model\\', $controller);
        if(class_exists($model)){
            return $model;
        }
        if(class_exists($model.'s')){
            return $model.'s';
        }
        return false;
    }
    protected function samevalidate()
    {
        //\think\facade\Config::load('extra/config', 'extra');
    }
    /**
     * [totabledata 转换为table可用的字段]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function totabledata($data)
    {
        $fields = ($this->_model)::getTableFields();
        foreach($data as $k=>$v)
        {
            if(!in_array($k,$fields)){
                unset($data[$k]);
            }
        }
        return $data;
    }
}
